const Controller = require('egg').Controller;
const createToken = require('../../util/jwt-strategy');  
const Result = require('../../util/result');
class UserController extends Controller {
  async findalluser() {
    const { ctx } = this;
    //ctx.body = await ctx.model.Students.findAll();
    const res = await ctx.service.user.findAlluser()
    ctx.body=res
  }

  async userLogin(){
    const {ctx}=this
    const {username,password}=ctx.request.body
    const user =await ctx.service.user.findOneUser(username,password)
    if(user){
        const{id}=user;
        const params={id}
        //jwt生成token
        const jwtToken=createToken(params);
        ctx.body=new Result ({
          token:jwtToken,
          id,
        },
        '登录成功'
        )
    }
  }
  async getUserArticle(){
    const {ctx}=this
    const {username}=ctx.request.body
    const articleArray =await ctx.service.user.findUserArticle(username)
    ctx.body=articleArray
  }
//   async addstudent() {
//     let str 
//     const { ctx } = this;
//     //ctx.body = await ctx.model.Students.findAll();
//       let {id,name, sex, age } = ctx.request.body
//       // console.log(name)
//       // console.log(id)
//       let result = await ctx.service.base._addstudent({id,name, age,sex  })
//       if (result === 'Server error') str='添加失败';
//       else  str='插入成功'
//     ctx.body=str
//   }
  // async findallteachers({id}) {
  //   const { ctx } = this;
  //   ctx.body = await ctx.service.base._deleteteachers({id});
  // }

}


module.exports = UserController;
